import { Component } from '@angular/core';
import { Service } from '../app/server/server';
import { Http } from '@angular/http';
import { Router,ActivatedRoute, Params } from '@angular/router';
import {Location} from '@angular/common';

declare var $: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})

export class AppComponent  {
  title = 'app';
  constructor(
    public router:Router, 
    public server : Service,
    private activatedRoute:ActivatedRoute,
    private location:Location
  ) {}

 

  ngOnInit() {
    $("#loding").show();
    if(this.server.exitMaxTime){
      this.server.openTime(this.server.exitMaxTime);
      //mousemove
      $('body').on('keydown mousedown', (e)=> {
        if(e.target.className.indexOf("automaticMp") < 0){
          this.server.exitTime = this.server.exitMaxTime; // reset
        }
      });
    }
    this.one1 = this.server.eixtTimeEmit.subscribe((value: any) => {
      if(value == 10){
        this.server.automatic = true;
      }else{
        this.router.navigateByUrl('login');
        this.server.automatic = false;
      }
      
    });
  }

  ngAfterContentInit(){
    $("#loding").hide();
    
  }

  public one1:any;
  ngOnDestroy(){
    if(this.one1){
      this.one1.unsubscribe();
    }
  }

  ngDoCheck(){
    if(this.location.path() == "/login"){
      clearInterval(this.server.intervalId);
    }
  }

  public yesOrNo(val){
    this.server.automatic = false;
    if(val == 1){
      this.router.navigateByUrl('login');
    }else{
      clearInterval(this.server.intervalId);
      this.server.openTime(this.server.exitMaxTime);
    }
  }

}
